<template>
	<view>
		<view class="search-box">
			<uni-search-bar @confirm="search" @input="input" ></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResults.length!==0">
			<view class="sugg-item" v-for="(item,index) in searchResults" :key="index" @click="gotoDetail(item.goods_id)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16" ></uni-icons>
				 <!-- <uni-icons type="arrowright" size="16"></uni-icons> -->
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title"> 
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag style="margin-top: 5px;margin-right: 5px;" :text="item"
				 v-for="(item,index) in history" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,
				kw:'',
				searchResults:[], //搜索结果列表
				historyList:['apple','huawei','dog']//搜索热词
			}
		},
		onLoad() {
			this.historyList= JSON.parse(uni.getStorageSync('kw')||[])
		},
		computed:{
			history(){
				return [...this.historyList].reverse()
			}
		},
		methods: {
			input(e){
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
					this.kw=e
					// console.log(e);
					this.getSearchList()
					
				},500)
			},
			/* 根据搜索关键词 搜索列表 */
			async getSearchList(){
				console.log(this.kw);
				if(this.kw===''){
					this.searchResults=[]
					return
				}
				 const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
				 if(res.meta.status!==200){
					 this.searchResults=[]
					 uni.$showMsg()
					 return
				 }
				 this.searchResults=res.message
				 this.saveHistoryList()
				 console.log(this.searchResults);
			},
			gotoDetail(id){
				// console.log(id);
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+id
				})
			},
			saveHistoryList(){
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				// set.delete(this.kw)
				// set.delete(this.kw)
				set.add(this.kw)
				this.historyList=Array.from(set)
				// this.historyList=
				// 来一个本地存储 这么获取数据
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			clearHistory(){
				this.historyList=[]
				uni.setStorageSync('kw','[]')
			},
			gotoGoodsList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+item
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.uni-searchbar {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  position: relative;
  padding: 16rpx;
  /* 将默认的 #FFFFFF 改为 #C00000 */
  background-color: #c00000;
}
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list{
	padding: 0 15rpx;
	.sugg-item{
		font-size: 12px;
		padding: 13px 0;
		display: flex;
		border-bottom: 1px solid #efefef;
		align-items: center;
		justify-content: space-between;
		.goods-name{
			white-space: nowrap;//文字不换行
			overflow: hidden;//移除隐藏
			text-overflow: ellipsis;// 省略号...
			margin-right: 5px;
		}
	}
}
.history-box{
	// display: flex;
	padding: 0 5px;
	.history-title{
		display: flex;
		justify-content: space-between;
		font-size: 14px;
		height: 40px;
		// line-height: 40px;
		align-items: center;
		border-bottom: 1px solid #efefef;
	}
	// justify-content: space-between;
	.history-list{
		display: flex;
		flex-wrap: wrap;
		.uni-tag{
			margin-right: 5px;
			margin-top: 5px;
			// background-color: red; 样式隔离了
		}
	}
}
</style>
